package com.tablefood.dao.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.tablefood.dao.ProfileDao;
import com.tablefood.domain.ShopProfile;

public class ProfileDaoImpl implements ProfileDao {

	@Resource
	private SessionFactory sessionFactory;   
	
	@Override
	public ShopProfile insertProfile(ShopProfile profile) {
		sessionFactory.getCurrentSession().save(profile);  
		return profile;
	}

	@Override
	public ShopProfile updateProfile(ShopProfile profile) {
		sessionFactory.getCurrentSession().update(profile);
		return profile;
	}

	@Override
	public List searchOneProfile(String userid) {
		
		String sql = "from ShopProfile as user where user.shopUser.userid=?";
		System.out.println("查找用户基本语句："+sql);
		
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, userid);
		
		System.out.println("---查询结果----");
		System.out.println("是否有结果query.list().size()："+query.list().size()); 
		System.out.println("返回result");
		if(query.list().size()!=0){
			List result = query.list(); 
			return result;
		}else{
			return null ;
		}
	}
	
	@Override
	public List searchProfileByUsername(String username)
	{
		// TODO Auto-generated method stub
		String Hql = 
				"        from " +
				"            ShopProfile as profile" +
				"         where" +
				"             profile.shopUser.username=?";
		System.out.println("查找用户基本语句："+Hql);
		
		Query query = sessionFactory.getCurrentSession().createQuery(Hql); 
		query.setString(0, username);
		
		System.out.println("-------");
		System.out.println("是否有结果："+query.list().size()); 
		System.out.println("返回result");
		List result = query.list(); 
		
		return result;
	}

}
